cc.Class({
    extends: cc.Component,

    properties: {
        diamond:cc.Label,
        money:cc.Label,
        time:cc.Label,
        price:cc.Label,
        updateBtn:cc.Node,
        wait:cc.Node,
        show:cc.Sprite,
        Flash:cc.Node,
        hamburger:[cc.SpriteFrame],
        meat:[cc.SpriteFrame],
        lettuce:[cc.SpriteFrame],
        onion:[cc.SpriteFrame],
        cheese:[cc.SpriteFrame],
        tomato:[cc.SpriteFrame],
        coffee:[cc.SpriteFrame],
        chengzhi:[cc.SpriteFrame],
        kele:[cc.SpriteFrame],
        plate:cc.SpriteFrame,
        plateUpdate:cc.SpriteFrame,
        bake:cc.SpriteFrame,
        bakeUpdate:cc.SpriteFrame,
    },
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {
        this.updateBtn.on('click',function(){
            cc.voicePlay.playEffect('click');
            this.loadShow();
            if( this.propName === 'plate' ||  this.propName === 'bake' ){
                if(!cc.configData.lock[this.propName].isUpdate){
                    if(cc.configData.money < this.moneyUpdate || cc.configData.diamond < this.diamondUpdate){
                        cc.find('Canvas/alert').active = true;
                        this.loadHide();//钱不够
                        return;
                    }
                    cc.configData.money -= this.moneyUpdate;
                    cc.configData.diamond -= this.diamondUpdate;
                    cc.configData.lock[this.propName].isUpdate = true;
                    cc.sys.localStorage.setItem("money",cc.configData.money);
                    cc.sys.localStorage.setItem("diamond",cc.configData.diamond);
                    if(CC_JSB){
                        this.loadHide();//升级成功
                        cc.find('Canvas').emit('money')
                        cc.find('Canvas').emit('diamond')
                        this.show.spriteFrame = this[this.propName+'Update'];
                        this.Flash.active = true;
                    }else{
                        cc.sdkbox.setDataAsync(cc.configData,function(){
                        this.loadHide();//升级成功
                        cc.find('Canvas').emit('money')
                        cc.find('Canvas').emit('diamond')
                        this.show.spriteFrame = this[this.propName+'Update'];
                        this.Flash.active = true;
                        }.bind(this));
                    }
                }else{
                    this.loadHide();//升级到顶
                    return;
                }
            }else{
                if(cc.configData.material[this.propName].level < 2){
                    if(cc.configData.money < this.moneyUpdate || cc.configData.diamond < this.diamondUpdate){
                        cc.find('Canvas/alert').active = true;
                        this.loadHide();//钱不够
                        return;
                    }
                    cc.configData.money -= this.moneyUpdate;
                    cc.configData.diamond -= this.diamondUpdate;
                    cc.configData.material[this.propName].level += 1;
                    cc.sys.localStorage.setItem("money",cc.configData.money);
                    cc.sys.localStorage.setItem("diamond",cc.configData.diamond);
                    if(CC_JSB){
                        this.loadHide();//升级成功
                        cc.find('Canvas').emit('money')
                        cc.find('Canvas').emit('diamond')
                        this.init(this.jsonData,this.propName);
                        this.Flash.active = true;
                    }else{
                        cc.sdkbox.setDataAsync(cc.configData,function(){
                            this.loadHide();//升级成功
                            cc.find('Canvas').emit('money')
                            cc.find('Canvas').emit('diamond')
                            this.init(this.jsonData,this.propName);
                            this.Flash.active = true;
                        }.bind(this))
                    };
                }else{
                    this.loadHide();//升级到顶
                    return;
                    // this.node.destroy();
                }
            }
            this.scheduleOnce(function(){
                this.Flash.active = false;
            },1)
        },this)
    },
    loadShow(){
       this.updateBtn.active = false;
       this.wait.active = true;
    },
    loadHide(){
        this.updateBtn.active = true;
        this.wait.active = false;
    },
    init(json,name){
        this.propName = name;
        this.jsonData = json;
        if( name === 'plate' ||  name === 'bake' ){
            this.time.string = '';
            this.time.node.parent.active = false;
            this.price.string = '';
            this.price.node.parent.active = false;
            this.diamond.string = json.levelMaterial[1];
            this.money.string = json.levelMaterial[0];
            this.diamondUpdate = json.levelMaterial[1];
            this.moneyUpdate = json.levelMaterial[0];
            this.show.spriteFrame = this[name];
           return;
        }
     this.show.spriteFrame = this[name][json.level];
     this.time.string = json.time[json.level] + 's';
     this.price.string = '$' + json.price[json.level];
     if(json.level<2){
         this.diamond.string = json.levelMaterial[json.level][1];
         this.money.string = json.levelMaterial[json.level][0];
         this.diamondUpdate = json.levelMaterial[json.level][1];
         this.moneyUpdate = json.levelMaterial[json.level][0];
     }else{
         this.diamond.string = 0;
         this.money.string = 0;
         this.diamondUpdate = 0;
         this.moneyUpdate = 0;
     }

     //if(name == ){}
    },
    // update (dt) {},
});
